package com.bringway.p2p.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Repository;

import net.sf.json.JSONObject;

import com.bringway.framework.dao.P2PJdbcDao;
import com.bringway.framework.util.DateUtils;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.global.util.UserUtil;
import com.bringway.p2p.dao.TxDao;


/**
 * 
 * @author zhai
 * @version 创建时间：2015年12月16日 上午9:34:42
 * @功能：提取现金界面
 */


@Repository
public class TxDaoimpl extends P2PJdbcDao implements TxDao {
	
	Logger logger = Logger.getLogger(TxDaoimpl.class);

	/**
	 * @author xuef
	 * TxDao 的实现类
	 * 获取提现的查询数据
	 */
	public List<Map<String, Object>> queryTxList(JSONObject params) {
		int start = params.optInt("start");
		int limit = params.optInt("limit");
		String startDate = params.optString("startDate");
		String endDate = params.optString("endDate");
		String sjhm=params.optString("sjhm");
		String gyh=params.optString("gyh");
		String txState=params.optString("txState");
		if(StringUtils.isNotEmpty(startDate)){
			startDate=startDate+" 00:00:00";
		}
		if(StringUtils.isNotEmpty(endDate)){
			endDate=endDate+" 23:59:59";
		}
		//2017-02-10 新增处理日期查询
		String operStartDate = params.optString("operStartDate");
		String operEndDate = params.optString("operEndDate");
		if(StringUtils.isNotEmpty(operStartDate)){
			operStartDate=operStartDate+" 00:00:00";
		}
		if(StringUtils.isNotEmpty(operEndDate)){
			operEndDate=operEndDate+" 23:59:59";
		}
		
		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("START", start);
		paramMap.put("LIMIT", limit);
		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);
		paramMap.put("SJHM", sjhm);
		paramMap.put("GYH", gyh);
		sql.append(" SELECT ID, GYH, TXJE,SXF,TXSJ,TX_STATE, DJJE,KYJE,ZHYE,SJHM, CardName, YHKHM, KHH, KHHSZD, OPER_GYH, date_format(OPER_DATE,'%Y-%m-%d %H:%i:%s') OPER_DATE  ");
		sql.append(" ,SFZHM,YHMC ");
		sql.append(" FROM v_p2p_tx where 1=1 ");
		if(StringUtils.isNotEmpty(startDate)){
			sql.append(" AND TXSJ >= :startDate");
		}
		if(StringUtils.isNotEmpty(endDate)){
			sql.append(" AND TXSJ <= :endDate");
		}
		
		if(StringUtils.isNotEmpty(sjhm)){
			sql.append(" AND SJHM like "+ "'%"+sjhm+"%'");
		}
		if(StringUtils.isNotEmpty(gyh)){
			sql.append(" AND GYH like "+ "'%"+gyh+"%'");
		}
		if(StringUtils.isNotEmpty(txState)){
			sql.append(" AND TX_STATE = :TX_STATE ");
			paramMap.put("TX_STATE", txState);
		}
		//2017-02-10 新增处理日期查询
		if(StringUtils.isNotEmpty(operStartDate)){
			sql.append(" AND OPER_DATE >= :OPER_START_DATE");
			paramMap.put("OPER_START_DATE", operStartDate);
		}
		if(StringUtils.isNotEmpty(operEndDate)){
			sql.append(" AND OPER_DATE <= :OPER_END_DATE");
			paramMap.put("OPER_END_DATE", operEndDate);
		}
		
		sql.append(" order by TXSJ desc ");
		if(0!=limit && !"export".equals(params.opt("queryType"))){//非导出类型，且分页不为0，才分页
			sql.append(" LIMIT :START,:LIMIT ");
		}
		logger.info("queryTxList sql : " + sql.toString());
		logger.info("queryTxList paramMap : " + paramMap.toString());
		return this.getNamedParameterJdbcTemplate().queryForList(sql.toString(), paramMap);

	}

	/**
	 * @author xuef
	 * 获取提现查询的数据个数
	 */
	@SuppressWarnings("deprecation")
	@Override
	public int getTxCount(JSONObject params) {
		String startDate = params.optString("startDate");
		String endDate = params.optString("endDate");
		String sjhm=params.optString("sjhm");
		String gyh=params.optString("gyh");
		String txState=params.optString("txState");
		if(StringUtils.isNotEmpty(startDate)){
			startDate=startDate+" 00:00:00";
		}
		if(StringUtils.isNotEmpty(endDate)){
			endDate=endDate+" 23:59:59";
		}
		//2017-02-10 新增处理日期查询
		String operStartDate = params.optString("operStartDate");
		String operEndDate = params.optString("operEndDate");
		if(StringUtils.isNotEmpty(operStartDate)){
			operStartDate=operStartDate+" 00:00:00";
		}
		if(StringUtils.isNotEmpty(operEndDate)){
			operEndDate=operEndDate+" 23:59:59";
		}
		StringBuffer sql = new StringBuffer();	
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);
		paramMap.put("SJHM", sjhm);
		paramMap.put("GYH", gyh);
		sql.append(" SELECT COUNT(*) CT ");
		sql.append(" FROM v_p2p_tx where 1=1 ");
		if(StringUtils.isNotEmpty(startDate)){
			sql.append(" AND TXSJ >= :startDate");
		}
		if(StringUtils.isNotEmpty(endDate)){
			sql.append(" AND TXSJ <= :endDate");
		}
		if(StringUtils.isNotEmpty(sjhm)){
			sql.append(" AND SJHM like "+ "'%"+sjhm+"%'");
		}
		if(StringUtils.isNotEmpty(gyh)){
			sql.append(" AND GYH like "+ "'%"+gyh+"%'");
		}
		if(StringUtils.isNotEmpty(txState)){
			sql.append(" AND TX_STATE = :TX_STATE ");
			paramMap.put("TX_STATE", txState);
		}
		//2017-02-10 新增处理日期查询
		if(StringUtils.isNotEmpty(operStartDate)){
			sql.append(" AND OPER_DATE >= :OPER_START_DATE");
			paramMap.put("OPER_START_DATE", operStartDate);
		}
		if(StringUtils.isNotEmpty(operEndDate)){
			sql.append(" AND OPER_DATE <= :OPER_END_DATE");
			paramMap.put("OPER_END_DATE", operEndDate);
		}
		logger.info("getTxCount sql : " + sql.toString());
		logger.info("getTxCount paramMap : " + paramMap.toString());
		return this.getNamedParameterJdbcTemplate().queryForInt(sql.toString(), paramMap);
	}

	@Override
	public String getTxSum(JSONObject params) {
		int start = params.optInt("start");
		int limit = params.optInt("limit");
		String startDate = params.optString("startDate");
		String endDate = params.optString("endDate");
		String sjhm=params.optString("sjhm");
		String gyh=params.optString("gyh");
		String txState=params.optString("txState");
		if(StringUtils.isNotEmpty(startDate)){
			startDate=startDate+" 00:00:00";
		}
		if(StringUtils.isNotEmpty(endDate)){
			endDate=endDate+" 23:59:59";
		}
		//2017-02-10 新增处理日期查询
		String operStartDate = params.optString("operStartDate");
		String operEndDate = params.optString("operEndDate");
		if(StringUtils.isNotEmpty(operStartDate)){
			operStartDate=operStartDate+" 00:00:00";
		}
		if(StringUtils.isNotEmpty(operEndDate)){
			operEndDate=operEndDate+" 23:59:59";
		}
		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("startDate", startDate);
		paramMap.put("endDate", endDate);
		paramMap.put("SJHM", sjhm);
		paramMap.put("GYH", gyh);
		paramMap.put("START", start);
		paramMap.put("LIMIT", limit);
		sql.append(" SELECT  sum(round(TXJE,2)) as SumJe ");
		sql.append(" FROM v_p2p_tx where 1=1 ");
		if(StringUtils.isNotEmpty(startDate)){
			sql.append(" AND TXSJ >= :startDate");
		}
		if(StringUtils.isNotEmpty(endDate)){
			sql.append(" AND TXSJ <= :endDate");
		}
		
		if(StringUtils.isNotEmpty(sjhm)){
			sql.append(" AND SJHM like "+ "'%"+sjhm+"%'");
		}
		if(StringUtils.isNotEmpty(gyh)){
			sql.append(" AND GYH like "+ "'%"+gyh+"%'");
		}
		if(StringUtils.isNotEmpty(txState)){
			sql.append(" AND TX_STATE = :TX_STATE ");
			paramMap.put("TX_STATE", txState);
		}
		//2017-02-10 新增处理日期查询
		if(StringUtils.isNotEmpty(operStartDate)){
			sql.append(" AND OPER_DATE >= :OPER_START_DATE");
			paramMap.put("OPER_START_DATE", operStartDate);
		}
		if(StringUtils.isNotEmpty(operEndDate)){
			sql.append(" AND OPER_DATE <= :OPER_END_DATE");
			paramMap.put("OPER_END_DATE", operEndDate);
		}
		logger.info("getTxSum sql : " + sql.toString());
		logger.info("getTxSum paramMap : " + paramMap.toString());
		return this.getNamedParameterJdbcTemplate().queryForObject(sql.toString(), paramMap, String.class);
	}

	@Override
	public void updateTxState(String id, String txState) {
		SysUserDetails sysUserDetails=(SysUserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String currentUserName = "";
		if(null!=sysUserDetails){
			currentUserName = sysUserDetails.getGyh();
		}
		StringBuffer sql = new StringBuffer();
		Map<String, String> paramMap = new HashMap<>();
		sql.append(" UPDATE T_ZHFW_TX SET TX_STATE = :TX_STATE, OPER_GYH=:OPER_GYH, OPER_DATE=:OPER_DATE ");
		sql.append("  WHERE ID = :ID AND TX_STATE = '00' ");
		paramMap.put("OPER_GYH", currentUserName);
		paramMap.put("OPER_DATE", DateUtils.getCurrentTimeStr());
		paramMap.put("ID", id);
		paramMap.put("TX_STATE", txState);
		logger.info("updateTxState sql is : " + sql.toString());
		logger.info("updateTxState paramMap is : " + paramMap.toString());
		this.getNamedParameterJdbcTemplate().update(sql.toString(), paramMap);
	}

	@Override
	public Map<String, Object> getTxInfo(String id) {
		StringBuffer sql = new StringBuffer();
		Map<String, String> paramMap = new HashMap<>();
		paramMap.put("ID", id);
		sql.append(" select t1.ID,t1.GYH,t1.TXJE,t1.YHKHM,t1.SXF,t1.TXSJ,t1.TX_STATE ,t2.SJHM ");
		sql.append(" from t_zhfw_tx t1, t_zhfw_user t2 where t1.GYH = t2.USER_NAME ");
		sql.append(" AND t1.ID = :ID");
		logger.info("getTxInfo getTxInfo is : " + sql.toString());
		logger.info("getTxInfo getTxInfo is : " + paramMap.toString());
		return this.getNamedParameterJdbcTemplate().queryForMap(sql.toString(), paramMap);
	}

	
	
}
